<?php

namespace app\index\controller;

use app\admin\model\Dep_customer;
use think\Controller;
use app\admin\model\Category;
use think\Db;
use think\Request;
use think\Session;
use think\Cookie;
use app\admin\model\Look;
use app\index\controller\Base;

class Personal extends Base
{

    private $redis;
    public function _initialize()
    {
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1', '6379');
    }

    //得到栏目
    public function getCate(){
        $cate = Category::all(function($query){
            $query->where('pid', 0)->limit(10);
        });
        $cates = [];
        $num = 0;
        foreach ($cate as $val){
            $pid = $val->getData()['id'];
            $cates[$num] = $val->getData();
            $cates[$num]['cate2'] = Db::name('category')->where('pid', '=', $pid)->select();
            $num ++;
        }
        return $cates;
    }


    public function index()
    {
        $cates = $this->getCate();
        $cus = session('user');
        $cus_dep = Dep_customer::get($cus['id'])->getData();
        $customer = array_merge($cus,$cus_dep);
        //var_dump($customer);die();
        return view('personal/index',['cate'=>$cates, 'customer'=>$customer]);
    }

    public function edit()
    {
        $info = input('post.');
        $id = $info['id'];

        $arr['name'] = $info['name'];
        $arr['sex'] = intval($info['sex']);
        $file = request()->file('head');
        if ($file){
            $res = $file->move(ROOT_PATH  . 'public/static/upload/head');
            if($res){
                $arr['headimg'] = $res->getSaveName();
                Session::set('user.figureurl',"/static/upload/head/".$arr['headimg']);
            }else{
                // 上传失败获取错误信息
                return  $file->getError();
            }
        }

        $cus_dep = new Dep_customer();
        $result = $cus_dep->save($arr,['id'=>$id]);

        if ($result > 0){
            
            Session::set('user.nickname',$arr['name']);
            $this->success('修改成功',url('index/personal/index'));
        }else{
            $this->error('修改失败',url('index/personal/index'));
        }
    }

    //加载评论页面
    public function  review()
    {
        $looks = Look::all();
        $cates = $this->getCate();
        $num = $this->redis->get('nextid');
        $reviews = [];
        if($num){

            for($i=0;$i<$num;$i++){
                $reviews[] = $this->redis->hgetall('user:'.($i+1));
            }
    
        }
        
        $reviews = array_reverse($reviews);


        return view('review/index',['cate'=>$cates, 'reviews'=>$reviews, 'looks'=>$looks]);
    }



    //退出登录
    public function exit()
    {
        Session::clear();
        Cookie::set('token_jwt','value',-3600);
        $this->success('已退出',url('index/index/index'));
        return;
    }

    //发表留言并判断是否登录
    public function isLogin(Request $request)
    {
        $arr = [];
        $con = $request->post('content');
        if (!empty(Session::get('user'))){
            $reviewid = $this->redis->incr('nextid');
            $this->redis->hmset("user:".$reviewid,array("id"=>$reviewid,
                "nickname"=>\session('user')['nickname'],
                "figureurl"=>\session('user')['figureurl'],
                'content'=>$con,
                're_time'=>time()));
            $arr['status'] = true;
            $arr['message'] = '谢谢你的留言！';
            $arr['nickname'] = \session('user')['nickname'];
            $arr['figureurl'] = \session('user')['figureurl'];
            $arr['time'] = Date('Y:m:d H:m:s',time());
            return json($arr);
        }elseif (!empty(Session::get('qq'))){
            $reviewid = $this->redis->incr('nextid');
            $this->redis->hmset("user:".$reviewid,array("id"=>$reviewid,
                "nickname"=>\session('qq')['nickname'],
                "figureurl"=>\session('qq')['figureurl'],
                'content'=>$con,
                're_time'=>time()));
            $arr['status'] = true;
            $arr['message'] = '谢谢你的留言！';
            $arr['nickname'] = \session('qq')['nickname'];
            $arr['figureurl'] = \session('qq')['figureurl'];
            $arr['time'] = Date('Y:m:d H:m:s',time());
            return json($arr);
        }else{
            $arr['status'] = false;
            $arr['message'] = '你还没登录！';
            return json($arr);
        }
    }
}
